package cn.com.inossem.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.adapter.ProfessionalAdapter;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.model.ProfessionalInfoBean;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 职业背景
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class ProfessionalActivity extends RequestActivity {

	LinearLayout llayout;

	TextView addcompany, savedata, del;
	List<ProfessionalInfoBean> messageInfo;
	List<ProfessionalInfoBean> Xuanzhongmessage;
	ProfessionalAdapter adapter;

	ListView listview;

	ImageView back;

	public int pos = 0;

	boolean isbianji = true;

	public boolean isShuaxin = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);

		// ProfessionalInfoBean pfib = new ProfessionalInfoBean();
		// pfib.name = "撒旦教";
		// pfib.yearname = "10年";
		// pfib.zhiwei = "总经理";
		// list.add(pfib);
		messageInfo = new ArrayList<ProfessionalInfoBean>();
		Xuanzhongmessage = new ArrayList<ProfessionalInfoBean>();
		initView();
		launchRequest(WooRequestFactory.getProfessional());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {

		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		del = (TextView) this.findViewById(R.id.del);
		del.setOnClickListener(this);

		listview = (ListView) this.findViewById(R.id.listview);
		// adapter = new ProfessionalAdapter(this, list,Xuanzhongmessage);
		// listview.setAdapter(adapter);

		savedata = (TextView) this.findViewById(R.id.savedata);
		savedata.setOnClickListener(this);
	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle.containsKey(WooRequestFactory.RESPONSE_PROFESSIONAL_DATA)) {
			messageInfo = new ArrayList<ProfessionalInfoBean>();
			Xuanzhongmessage = new ArrayList<ProfessionalInfoBean>();
			List<ProfessionalInfoBean> retList = bundle
					.getParcelableArrayList(WooRequestFactory.RESPONSE_PROFESSIONAL_DATA);
			System.out.println("retList长度：" + retList.size());
			if (retList != null)
				messageInfo = retList;
			System.out.println("messageInfo长度：" + messageInfo.size());
			ProfessionalInfoBean pfib = new ProfessionalInfoBean();
			pfib.setComName("");
			pfib.isselected = 0;
			messageInfo.add(pfib);
			System.out.println("长度：" + messageInfo.size());
			adapter = new ProfessionalAdapter(this, messageInfo,
					Xuanzhongmessage);
			listview.setAdapter(adapter);

			isbianji = true;
			savedata.setText("编辑");
			if (adapter != null) {
				adapter.hiddenButton();
				del.setVisibility(View.GONE);
			}

		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS)) {
			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS) == 1) {
				if (isShuaxin)
					launchRequest(WooRequestFactory.getProfessional());
			} else {
				Toast.makeText(this, "修改失败!", Toast.LENGTH_LONG).show();
			}

		} else if (bundle.containsKey(WooRequestFactory.BUNDLE_EXTRA_DELGONGSI)) {
			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_DELGONGSI) == 1) {
				System.out.println("删除成功！");
				launchRequest(WooRequestFactory.getProfessional());
			}
		}
	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.professional_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			resultStr();
			break;
		case R.id.savedata:// 保存
			if (isbianji) {
				isbianji = false;
				savedata.setText("保存");
				if (adapter != null) {
					adapter.showButton();
					del.setVisibility(View.VISIBLE);
				}
			} else {
				isbianji = true;
				savedata.setText("编辑");
				if (adapter != null) {
					adapter.hiddenButton();
					del.setVisibility(View.GONE);
				}
			}
			break;

		case R.id.del:
			if (Xuanzhongmessage.size() > 0) {
				String deleteUserExpCom = Xuanzhongmessage.get(0).getId();
				for (int i = 1; i < Xuanzhongmessage.size(); i++) {
					deleteUserExpCom += "," + Xuanzhongmessage.get(i).getId();
				}
				launchRequest(WooRequestFactory
						.delProfessional(deleteUserExpCom));
			}

			break;
		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			break;
		case 2:
			break;
		case 4:
			int id = data.getIntExtra("id", 0);
			String name = data.getStringExtra("name");

			messageInfo.get(pos).setPosition(id + "");
			adapter.notifyDataSetChanged();
			isShuaxin = false;
			launchRequest(WooRequestFactory.addProfessional(messageInfo
					.get(pos)));
			break;
		case 108:
			name = data.getStringExtra("name");
			messageInfo.get(pos).setPeriod(name);
			adapter.notifyDataSetChanged();
			isShuaxin = false;
			launchRequest(WooRequestFactory.addProfessional(messageInfo
					.get(pos)));
			break;
		case 110:
			name = data.getStringExtra("name");

			messageInfo.get(pos).setPosition(name);
			adapter.notifyDataSetChanged();
			isShuaxin = false;
			launchRequest(WooRequestFactory.addProfessional(messageInfo
					.get(pos)));
			break;
		case 10010:
			name = data.getStringExtra("name");
			String nameid = data.getStringExtra("nameid");

			System.out.println("name:" + name + "nameid:" + nameid);

			ProfessionalInfoBean bean = new ProfessionalInfoBean();
			bean.setComName(name);
			isShuaxin = true;
			if (!nameid.equals("")) {
				bean.setComID(Integer.parseInt(nameid));
				launchRequest(WooRequestFactory.addProfessional(bean));
			} else {
				launchRequest(WooRequestFactory.addProfessional(bean));
			}

			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			resultStr();
			return true;
		}

		return super.onKeyDown(keyCode, event);

	}

	public void resultStr() {
		Intent intent = new Intent();
		System.out.println(messageInfo.size()
				+ "messageInfo.get(0).getSchoolName():"
				+ messageInfo.get(0).getComName());
		if (messageInfo.size() == 2)
			intent.putExtra("name", messageInfo.get(0).getComName());
		else if (messageInfo.size() > 2)
			intent.putExtra("name", messageInfo.get(0).getComName() + "等"
					+ (messageInfo.size() - 1) + "个");
		else
			intent.putExtra("name", "");
		setResult(104, intent);
		finish();
	}
}
